My cosmetic company symbols
library(quantmod)
library(TTR)
library(binhf)
cosmetic_symbols <- c('EL','LRLCY','REV','SSDOY','ELF','COTY')
getSymbols(cosmetic_symbols, src='yahoo')
EL <- EL[!(rowSums(is.na(EL)))]
LRLCY <- LRLCY[!(rowSums(is.na(LRLCY)))]
REV <- REV[!(rowSums(is.na(REV)))]
SSDOY <- SSDOY[!(rowSums(is.na(SSDOY)))]
ELF <- ELF[!(rowSums(is.na(ELF)))]
COTY <- COTY[!(rowSums(is.na(COTY)))]
simple moving average (SMA)
attach(mtcars)
par(mfrow=c(6,1))
chartSeries(EL, theme="white", TA="addSMA(100)")
chartSeries(LRLCY, theme="white", TA="addSMA(100)")
chartSeries(REV, theme="white", TA="addSMA(100)")
chartSeries(SSDOY, theme="white", TA="addSMA(100)")
chartSeries(ELF, theme="white", TA="addSMA(100)")
chartSeries(COTY, theme="white", TA="addSMA(100)")
###multiple moving averages(EMA)
EL.EMA.10 <- EMA(EL$EL.Close, n=10 )
EL.EMA.50 <- EMA(EL$EL.Close, n=50)
EL.EMA.200 <- EMA(EL$EL.Close, n=200)
EL.Fast.Diff <- EL.EMA.10 - EL.EMA.50
EL.Slow.Diff <- EL.EMA.50 - EL.EMA.200
LRLCY.EMA.10 <- EMA(LRLCY$LRLCY.Close, n=10)
LRLCY.EMA.50 <- EMA(LRLCY$LRLCY.Close, n=50)
LRLCY.EMA.200 <- EMA(LRLCY$LRLCY.Close, n=200)
LRLCY.Fast.Diff <- LRLCY.EMA.10 - LRLCY.EMA.50
LRLCY.Slow.Diff <- LRLCY.EMA.50 - LRLCY.EMA.200
REV.EMA.10 <- EMA(REV$REV.Close, n=10 )
REV.EMA.50 <- EMA(REV$REV.Close, n=50)
REV.EMA.200 <- EMA(REV$REV.Close, n=200)
REV.Fast.Diff <- REV.EMA.10 - REV.EMA.50
REV.Slow.Diff <- REV.EMA.50 - REV.EMA.200
SSDOY.EMA.10 <- EMA(SSDOY$SSDOY.Close, n=10)
SSDOY.EMA.50 <- EMA(SSDOY$SSDOY.Close, n=50)
SSDOY.EMA.200 <- EMA(SSDOY$SSDOY.Close, n=200)
SSDOY.Fast.Diff <- SSDOY.EMA.10 - SSDOY.EMA.50
SSDOY.Slow.Diff <- SSDOY.EMA.50 - SSDOY.EMA.200
ELF.EMA.10 <- EMA(ELF$ELF.Close, n=10)
ELF.EMA.50 <- EMA(ELF$ELF.Close, n=50)
ELF.EMA.200 <- EMA(ELF$ELF.Close, n=200)
ELF.Fast.Diff <- ELF.EMA.10 - EL.EMA.50
ELF.Slow.Diff <- ELF.EMA.50 - EL.EMA.200
COTY.EMA.10 <- EMA(COTY$COTY.Close, n=10)
COTY.EMA.50 <- EMA(COTY$COTY.Close, n=50)
COTY.EMA.200 <- EMA(COTY$COTY.Close, n=200)
COTY.Fast.Diff <- COTY.EMA.10 - COTY.EMA.50
COTY.Slow.Diff <- COTY.EMA.50 - COTY.EMA.200
EL
chartSeries(EL, theme="white", TA="addEMA(50, col='black');addEMA(200, col='blue')")
addTA(EL.Fast.Diff, col='blue', type='h',legend="10-50 MA")
addTA(EL.Slow.Diff, col='red', type='h',legend="50-200 MA")
LRLCY
chartSeries(LRLCY, theme="white", TA="addEMA(50, col='black');addEMA(200, col='blue')")
addTA(LRLCY.Fast.Diff, col='blue', type='h',legend="10-50 MA")
addTA(LRLCY.Slow.Diff, col='red', type='h',legend="50-200 MA")
REV
chartSeries(REV, theme="white", TA="addEMA(50, col='black');addEMA(200, col='blue')")
addTA(REV.Fast.Diff, col='blue', type='h',legend="10-50 MA")
addTA(REV.Slow.Diff, col='red', type='h',legend="50-200 MA")
SSDOY
chartSeries(SSDOY, theme="white", TA="addEMA(50, col='black');addEMA(200, col='blue')")
addTA(SSDOY.Fast.Diff, col='blue', type='h',legend="10-50 MA")
addTA(SSDOY.Slow.Diff, col='red', type='h',legend="50-200 MA")
ELF
chartSeries(ELF, theme="white", TA="addEMA(50, col='black');addEMA(200, col='blue')")
addTA(ELF.Fast.Diff, col='blue', type='h',legend="10-50 MA")
addTA(ELF.Slow.Diff, col='red', type='h',legend="50-200 MA")
COTY
chartSeries(COTY, theme="white", TA="addEMA(50, col='black');addEMA(200, col='blue')")
addTA(COTY.Fast.Diff, col='blue', type='h',legend="10-50 MA")
addTA(COTY.Slow.Diff, col='red', type='h',legend="50-200 MA")
###Trading With The Trend using EMA EL
# look for long entries
EL$Long_Trades <- ifelse(
EL.Slow.Diff > 0 &
EL.Fast.Diff > 0 &
shift(v=as.numeric(EL.Fast.Diff), places=1, dir="right") < 0, EL$EL.Close, NA)
# look for long exits (same thing but inverse signts)
EL$Short_Trades <- ifelse(
EL.Slow.Diff < 0 &
EL.Fast.Diff < 0 &
shift(v=as.numeric(EL.Fast.Diff), places=1, dir="right") > 0, EL$EL.Close, NA)
plot(EL$EL.Close)
points(EL$Long_Trades, col='blue', cex=1.5, pch=18)
points(EL$Short_Trades, col='red', cex=1.5, pch=18)
LRLCY
LRLCY$Long_Trades <- ifelse(
LRLCY.Slow.Diff > 0 &
LRLCY.Fast.Diff > 0 &
shift(v=as.numeric(LRLCY.Fast.Diff), places=1, dir="right") < 0, LRLCY$LRLCY.Close, NA)
# look for long exits (same thing but inverse signts)
LRLCY$Short_Trades <- ifelse(
LRLCY.Slow.Diff < 0 &
LRLCY.Fast.Diff < 0 &
shift(v=as.numeric(LRLCY.Fast.Diff), places=1, dir="right") > 0, LRLCY$LRLCY.Close, NA)
plot(LRLCY$LRLCY.Close)
points(LRLCY$Long_Trades, col='blue', cex=1.5, pch=18)
points(LRLCY$Short_Trades, col='red', cex=1.5, pch=18)
REV
REV$Long_Trades <- ifelse(
REV.Slow.Diff > 0 &
REV.Fast.Diff > 0 &
shift(v=as.numeric(REV.Fast.Diff), places=1, dir="right") < 0, REV$REV.Close, NA)
# look for long exits (same thing but inverse signts)
REV$Short_Trades <- ifelse(
REV.Slow.Diff < 0 &
REV.Fast.Diff < 0 &
shift(v=as.numeric(REV.Fast.Diff), places=1, dir="right") > 0, REV$REV.Close, NA)
plot(REV$REV.Close)
points(REV$Long_Trades, col='blue', cex=1.5, pch=18)
points(REV$Short_Trades, col='red', cex=1.5, pch=18)
SSDOY
SSDOY$Long_Trades <- ifelse(
SSDOY.Slow.Diff > 0 &
SSDOY.Fast.Diff > 0 &
shift(v=as.numeric(SSDOY.Fast.Diff), places=1, dir="right") < 0, SSDOY$SSDOY.Close, NA)
# look for long exits (same thing but inverse signts)
SSDOY$Short_Trades <- ifelse(
SSDOY.Slow.Diff < 0 &
SSDOY.Fast.Diff < 0 &
shift(v=as.numeric(SSDOY.Fast.Diff), places=1, dir="right") > 0, SSDOY$SSDOY.Close, NA)
plot(SSDOY$SSDOY.Close)
points(SSDOY$Long_Trades, col='blue', cex=1.5, pch=18)
points(SSDOY$Short_Trades, col='red', cex=1.5, pch=18)
ELF
ELF$Long_Trades <- ifelse(
ELF.Slow.Diff > 0 &
ELF.Fast.Diff > 0 &
shift(v=as.numeric(ELF.Fast.Diff), places=1, dir="right") < 0, ELF$ELF.Close, NA)
# look for long exits (same thing but inverse signts)
ELF$Short_Trades <- ifelse(
ELF.Slow.Diff < 0 &
ELF.Fast.Diff < 0 &
shift(v=as.numeric(ELF.Fast.Diff), places=1, dir="right") > 0, ELF$ELF.Close, NA)
plot(ELF$ELF.Close)
points(ELF$Long_Trades, col='blue', cex=1.5, pch=18)
## Warning in min(x): no non-missing arguments to min; returning Inf
## Warning in max(x): no non-missing arguments to max; returning -Inf
points(ELF$Short_Trades, col='red', cex=1.5, pch=18)
## Warning in min(x): no non-missing arguments to min; returning Inf
## Warning in min(x): no non-missing arguments to max; returning -Inf
COTY
COTY$Long_Trades <- ifelse(
COTY.Slow.Diff > 0 &
COTY.Fast.Diff > 0 &
shift(v=as.numeric(COTY.Fast.Diff), places=1, dir="right") < 0, COTY$COTY.Close, NA)
# look for long exits (same thing but inverse signts)
COTY$Short_Trades <- ifelse(
COTY.Slow.Diff < 0 &
COTY.Fast.Diff < 0 &
shift(v=as.numeric(COTY.Fast.Diff), places=1, dir="right") > 0, COTY$COTY.Close, NA)
plot(COTY$COTY.Close)
points(COTY$Long_Trades, col='blue', cex=1.5, pch=18)
points(COTY$Short_Trades, col='red', cex=1.5, pch=18)
####Trading With The Trend using ADX and VWAP ADX and VWAP
EL.ADX.20 <- ADX(EL,n=20)
LRLCY.ADX.20 <- ADX(LRLCY,n=20)
REV.ADX.20 <- ADX(REV,n=20)
SSDOY.ADX.20 <- ADX(SSDOY,n=20)
ELF.ADX.20 <- ADX(ELF,n=20)
COTY.ADX.20 <- ADX(COTY,n=20)
EL.VWAP.Slow <- VWAP(price=EL$EL.Close, volume=EL$EL.Volume, n=100)
EL.VWAP.Fast <- VWAP(price=EL$EL.Close, volume=EL$EL.Volume, n=20)
EL.VWAP.Diff <- EL.VWAP.Fast- EL.VWAP.Slow
LRLCY.VWAP.Slow <- VWAP(price=LRLCY$LRLCY.Close, volume=LRLCY$LRLCY.Volume, n=100)
LRLCY.VWAP.Fast <- VWAP(price=LRLCY$LRLCY.Close, volume=LRLCY$LRLCY.Volume, n=20)
LRLCY.VWAP.Diff <- LRLCY.VWAP.Fast- LRLCY.VWAP.Slow
REV.VWAP.Slow <- VWAP(price=REV$REV.Close, volume=REV$REV.Volume, n=100)
REV.VWAP.Fast <- VWAP(price=REV$REV.Close, volume=REV$REV.Volume, n=20)
REV.VWAP.Diff <- REV.VWAP.Fast- REV.VWAP.Slow
SSDOY.VWAP.Slow <- VWAP(price=SSDOY$SSDOY.Close, volume=SSDOY$SSDOY.Volume, n=100)
SSDOY.VWAP.Fast <- VWAP(price=SSDOY$SSDOY.Close, volume=SSDOY$SSDOY.Volume, n=20)
SSDOY.VWAP.Diff <- SSDOY.VWAP.Fast- SSDOY.VWAP.Slow
ELF.VWAP.Slow <- VWAP(price=ELF$ELF.Close, volume=ELF$ELF.Volume, n=100)
ELF.VWAP.Fast <- VWAP(price=ELF$ELF.Close, volume=ELF$ELF.Volume, n=20)
ELF.VWAP.Diff <- ELF.VWAP.Fast- ELF.VWAP.Slow
COTY.VWAP.Slow <- VWAP(price=COTY$COTY.Close, volume=COTY$COTY.Volume, n=100)
COTY.VWAP.Fast <- VWAP(price=COTY$COTY.Close, volume=COTY$COTY.Volume, n=20)
COTY.VWAP.Diff <- COTY.VWAP.Fast- COTY.VWAP.Slow
EL
# look for long entries
EL.Long_Trades <- ifelse(
EL.ADX.20$ADX > 30 &
EL.VWAP.Diff> 10, EL$EL.Close, NA)
# look for long entries
EL.Short_Trades <- ifelse(
EL.ADX.20$ADX > 30 &
EL.VWAP.Diff < 10, EL$EL.Close, NA)
plot(EL$EL.Close)
points(EL.Long_Trades, col='blue', cex=1.5, pch=18)
points(EL.Short_Trades, col='red', cex=1.5, pch=18)
LRLCY
LRLCY.Long_Trades <- ifelse(
LRLCY.ADX.20$ADX > 20 &
LRLCY.VWAP.Diff> 0, LRLCY$LRLCY.Close, NA)
LRLCY.Short_Trades <- ifelse(
LRLCY.ADX.20$ADX > 20 &
LRLCY.VWAP.Diff < 0, LRLCY$LRLCY.Close, NA)
plot(LRLCY$LRLCY.Close)
points(LRLCY.Long_Trades, col='blue', cex=1.5, pch=18)
points(LRLCY.Short_Trades, col='red', cex=1.5, pch=18)
REV
REV.Long_Trades <- ifelse(
REV.ADX.20$ADX > 20 &
REV.VWAP.Diff> 0, REV$REV.Close, NA)
REV.Short_Trades <- ifelse(
REV.ADX.20$ADX > 20 &
REV.VWAP.Diff < 0, REV$REV.Close, NA)
plot(REV$REV.Close)
points(REV.Long_Trades, col='blue', cex=1.5, pch=18)
points(REV.Short_Trades, col='red', cex=1.5, pch=18)
SSDOY
SSDOY.Long_Trades <- ifelse(
SSDOY.ADX.20$ADX > 20 &
SSDOY.VWAP.Diff> 0, SSDOY$SSDOY.Close, NA)
SSDOY.Short_Trades <- ifelse(
SSDOY.ADX.20$ADX > 20 &
SSDOY.VWAP.Diff < 0, SSDOY$SSDOY.Close, NA)
plot(SSDOY$SSDOY.Close)
points(SSDOY.Long_Trades, col='blue', cex=1.5, pch=18)
points(SSDOY.Short_Trades, col='red', cex=1.5, pch=18)
ELF
ELF.Long_Trades <- ifelse(
ELF.ADX.20$ADX > 20 &
ELF.VWAP.Diff> 0, ELF$ELF.Close, NA)
EL.Short_Trades <- ifelse(
ELF.ADX.20$ADX > 20 &
ELF.VWAP.Diff < 0, ELF$ELF.Close, NA)
plot(ELF$ELF.Close)
#points(ELF.Long_Trades, col='blue', cex=1.5, pch=18)
#points(ELF.Short_Trades, col='red', cex=1.5, pch=18)
COTY
COTY.Long_Trades <- ifelse(
COTY.ADX.20$ADX > 20 &
COTY.VWAP.Diff> 0, COTY$COTY.Close, NA)
COTY.Short_Trades <- ifelse(
COTY.ADX.20$ADX > 20 &
COTY.VWAP.Diff < 0, COTY$COTY.Close, NA)
plot(COTY$COTY.Close)
points(COTY.Long_Trades, col='blue', cex=1.5, pch=18)
points(COTY.Short_Trades, col='red', cex=1.5, pch=18)
####Simple function to show long and short decisions
ProfitLoss_Calculator <- function(objDF) {
# make column names generic so they can handle any symbol
colnames(objDF) <- c("Open", "High", "Low", "Close", "Volume", "Adjust","Long_Trades", "Short_Trades")
current_long <- 0
current_short <- 0
for (ind in seq(1,nrow(objDF))) {
if (objDF$Long_Trades[ind] !=0) {
# first trade should be an entry, last trade an exit
if ((current_long==0 & objDF$Long_Trades[ind] > 0) | (current_long !=0)) {
# next trade should be opposite sign of previous trade (entry -> exit)
if (sign(objDF$Long_Trades[ind]) != sign(current_long)) {
current_long <- as.numeric(objDF$Long_Trades[ind])
print(paste('Long', current_long))
}
}
if (current_long != as.numeric(objDF$Long_Trades[ind]))
objDF$Long_Trades[ind] <- 0
}
if (objDF$Short_Trades[ind] !=0) {
# first trade should be an entry
if ((current_short==0 & objDF$Short_Trades[ind] > 0) | (current_short !=0)) {
# next trade should be opposite sign of previous trade (entry -> exit)
if (sign(objDF$Short_Trades[ind]) != sign(current_short)) {
current_short <- as.numeric(objDF$Short_Trades[ind])
print(paste('Short', current_short))
}
}
if (current_short != as.numeric(objDF$Short_Trades[ind]))
objDF$Short_Trades[ind] <- 0
}
}
# trim to be even, if not add last close held in chart
if ((!length(objDF$Long_Trades[objDF$Long_Trades != 0])%% 2) == 0)
objDF$Long_Trades[length(objDF$Close)] <- -1 * objDF$Close[length(objDF$Close)]
if ((!length(objDF$Short_Trades[objDF$Short_Trades != 0])%% 2) == 0)
objDF$Short_Trades[length(objDF$Close)] <- -1 * objDF$Close[length(objDF$Close)]
print(paste('Final Longs:',round(sum(objDF$Long_Trades * -1),2)))
print(paste('Final Shorts:',round(sum(objDF$Short_Trades),2)))
# plot trade entries and exits
plot(objDF$Close, main='Trades')
#legend(1,95,c("long entry","long exits","short entry","short exit"),pch=c(16,15,14,13),cex=1.5)
points(ifelse(objDF$Long_Trades > 0, objDF$Long_Trades, NA), col='green', cex=1.5, pch=16)
points(ifelse(objDF$Long_Trades < 0, objDF$Long_Trades * -1, NA), col='red', cex=1.5, pch=15)
points(ifelse(objDF$Short_Trades > 0, objDF$Short_Trades, NA), col='blue', cex=1.5, pch=14)
points(ifelse(objDF$Short_Trades < 0, objDF$Short_Trades * -1, NA), col='orange', cex=1.5, pch=13)
}
long trade entry:green long trade exit:red short trade entry:blue short trade exit:orange
EL
# exits for longs
EL$Long_Trades <- ifelse(EL.Fast.Diff < 0, -1 * EL$EL.Close, EL$Long_Trades)
EL$Long_Trades[is.na(EL$Long_Trades)] <- 0
# exits for longs
EL$Short_Trades <- ifelse(EL.Fast.Diff > 0, -1 * EL$EL.Close, EL$Short_Trades)
EL$Short_Trades[is.na(EL$Short_Trades)] <- 0
ProfitLoss_Calculator(EL)
LRLCY
LRLCY$Long_Trades <- ifelse(LRLCY.Fast.Diff < 0, -1 * LRLCY$LRLCY.Close, LRLCY$Long_Trades)
LRLCY$Long_Trades[is.na(LRLCY$Long_Trades)] <- 0
# exits for longs
LRLCY$Short_Trades <- ifelse(LRLCY.Fast.Diff > 0, -1 * LRLCY$LRLCY.Close, LRLCY$Short_Trades)
LRLCY$Short_Trades[is.na(LRLCY$Short_Trades)] <- 0
ProfitLoss_Calculator(LRLCY)
REV
REV$Long_Trades <- ifelse(REV.Fast.Diff < 0, -1 * REV$REV.Close, REV$Long_Trades)
REV$Long_Trades[is.na(REV$Long_Trades)] <- 0
# exits for longs
REV$Short_Trades <- ifelse(REV.Fast.Diff > 0, -1 * REV$REV.Close, REV$Short_Trades)
REV$Short_Trades[is.na(REV$Short_Trades)] <- 0
ProfitLoss_Calculator(REV)
SSDOY
SSDOY$Long_Trades <- ifelse(SSDOY.Fast.Diff < 0, -1 * SSDOY$SSDOY.Close, SSDOY$Long_Trades)
SSDOY$Long_Trades[is.na(SSDOY$Long_Trades)] <- 0
# exits for longs
SSDOY$Short_Trades <- ifelse(SSDOY.Fast.Diff > 0, -1 * SSDOY$SSDOY.Close, SSDOY$Short_Trades)
SSDOY$Short_Trades[is.na(SSDOY$Short_Trades)] <- 0
ProfitLoss_Calculator(SSDOY)
ELF
ELF$Long_Trades <- ifelse(ELF.Fast.Diff < 0, -1 * ELF$ELF.Close, ELF$Long_Trades)
ELF$Long_Trades[is.na(ELF$Long_Trades)] <- 0
# exits for longs
ELF$Short_Trades <- ifelse(ELF.Fast.Diff > 0, -1 * EFL$ELF.Close, ELF$Short_Trades)
ELF$Short_Trades[is.na(ELF$Short_Trades)] <- 0
ProfitLoss_Calculator(ELF)
## Warning in min(x): no non-missing arguments to min; returning Inf
## Warning in max(x): no non-missing arguments to max; returning -Inf
## Warning in min(x): no non-missing arguments to min; returning Inf
## Warning in max(x): no non-missing arguments to max; returning -Inf
## Warning in min(x): no non-missing arguments to min; returning Inf
## Warning in max(x): no non-missing arguments to max; returning -Inf
## Warning in min(x): no non-missing arguments to min; returning Inf
## Warning in max(x): no non-missing arguments to max; returning -Inf
COTY
COTY$Long_Trades <- ifelse(COTY.Fast.Diff < 0, -1 * COTY$COTY.Close, COTY$Long_Trades)
COTY$Long_Trades[is.na(COTY$Long_Trades)] <- 0
# exits for longs
COTY$Short_Trades <- ifelse(COTY.Fast.Diff > 0, -1 * COTY$COTY.Close, COTY$Short_Trades)
COTY$Short_Trades[is.na(COTY$Short_Trades)] <- 0
ProfitLoss_Calculator(COTY)